layui.define(["element","form", "zTree"], function (exports) {
    var result = "";
    var $ = layui.$
        , zTree = layui.zTree;
    var CustomTree = function () {
            this.v = '0.1';
            this.result=result;
        }, _MOD = 'customTree';

    CustomTree.prototype.render = function (options) {
        var opts = options,
            //访问的url
        url = opts.url;
        //是否为复选框
        check=opts.check;
        //类型
        type = opts.type || "get";
        //值
        value=opts.value;
        //上级的id
        rootId=opts.rootId;
        //关键字
        elem=opts.elem;
        //isAll不为空的话则获取全部
        isAll=opts.isAll;

        //配置类
        var setting = {
            async: {
                enable: true,
                url: url,
                otherParam: ["isAll",isAll],
                type: type
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: rootId
                }
            },
            check: {
                enable: check === 'true' ? true : false,
                chkStyle: "checkbox" //显示样式为单选
            },
            callback: {
                onDblClick: function (event, treeId, treeNode) {
                    // var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    // $(obj.elem, parent.document).val(treeNode.id);
                    // $(obj.showElem, parent.document).children('input').val(treeNode.name);
                    // parent.layer.close(index);
                },
                onClick: function (event, treeId, treeNode) {
                    var currZtree = zTree.getZTreeObj(treeId);
                    var expandFlag = treeNode.open ? false : true;
                    currZtree.expandNode(treeNode, expandFlag, false, true);
                },
                onAsyncSuccess: function (event, treeId, treeNode) {
                    var currZtree = zTree.getZTreeObj(treeId);
                    var nodeList = currZtree.getNodes();
                    currZtree.expandNode(nodeList[0], true);

                    if (check === 'true') {
                        var pnodes = [];
                        var ids = value.split(',');
                        ids.forEach(function (item, index) {
                            var node = currZtree.getNodeByParam("id", item, null) || '';
                            node.checked = true;
                            currZtree.updateNode(node);
                            var pnode = currZtree.getNodeByParam("id", node.pId, null);
                            pnodes.push(pnode);
                        });
                        if (pnodes[0] != null) {
                            pnodes.forEach(function (node, index) {
                                currZtree.updateNode(node);
                                currZtree.expandNode(node, true);
                            });
                        }
                    } else {
                        var node = currZtree.getNodeByParam("id", value, null) || '';
                        currZtree.selectNode(node);
                        var pnode = currZtree.getNodeByParam("id", node.pId, null);
                        currZtree.expandNode(pnode, true);
                    }
                }
            }
        };
        result = zTree.init($(""+elem+""), setting);
        return result;
    };



    var customtree = new CustomTree();

    exports(_MOD, customtree);
});